package com.xjoyt.pay.master.controllers;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xjoyt.pay.controllers.AbstractController;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.interfaces.CompanyContract;
import com.xjoyt.pay.master.params.CompanyPageParam;
import com.xjoyt.pay.master.po.Company;
import com.xjoyt.pay.master.services.CompanyService;

@RestController
@RequestMapping("company")
public class CompanyController extends AbstractController implements CompanyContract {

	@Autowired
	private CompanyService service;

	@Override
	public Company get(@PathVariable("id") String id) {
		return service.get(id);
	}

	@Override
	public ExecuteResult<Company> save(@RequestBody Company param) {
		return service.save(param);
	}

	@Override
	public ExecuteResult<?> remove(@PathVariable("id") String id) {
		return service.delete(id);
	}

	@Override
	public ExecuteResult<?> removeAll(@RequestBody List<String> ids) {
		for (String id : ids) {
			service.delete(id);
		}
		return ExecuteResult.ok();
	}

	@Override
	public PageResult<Company> loadPage(@RequestBody PageParam<CompanyPageParam> param) {
		if (StringUtils.isBlank(param.order))
			param.order = "desc";
		if (StringUtils.isBlank(param.sort))
			param.sort = "addedDate";
		return service.loadPage(param);
	}

	@Override
	public List<Company> findAllById(@PathVariable("id") String id) {
		return service.findAllById(id);
	}

	@Override
	public List<Company> findAllParentById(@RequestBody List<String> ids) {
		return service.findAllParentById(ids);

	}

	@Override
	public Company getByDomain(@PathVariable("domain") String domain) {
		return service.getByDomain(domain);
	}

}
